﻿function Dialog(){
	this.uid="";
	this._$dialog=null;
	this._uid= function(){
		var S4= function(){return (((1+Math.random())*0x10000)|0).toString(16).substring(1);};
		return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());  
	};
	this._letDivCenter= function(){   
        var top = ($(window).height() - this._$dialog.height())/2;   
        var left = ($(window).width() - this._$dialog.width())/2;   
        var scrollTop = $(document).scrollTop();   
        var scrollLeft = $(document).scrollLeft();   
        this._$dialog.css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show(10);
	};
	this._defaultConfig= {width:300,height:200,type:1};
	this.openDialog= function(content,config,id){
		config= config?$.extend(this._defaultConfig,config):this._defaultConfig;
		this.uid= id?id:this._uid();
		var digDiv= this._gtContent(content,config);
		$("body").append(digDiv);
		this._show();
		this._initBindEvent();
		return this;
	};
	this.alert= function(content){
		this.openDialog(content,{width:250,height:150,type:2});
	}
	this._gtContent= function(con,config){
		var digDiv= '<div id="'+this.uid+'"'+ 
						' style="font-size:12px;display:none;background-color:azure;border: solid 1px #ccc;position:absolute;width:'+config.width+'px;height:'+config.height+'px;">'+
						'<div style="width:100%;height:30px;line-height:30px;float:left;background:aliceblue;text-align: right;">'+
							'<a style="cursor:pointer;margin-right:5px;" id="'+'close_'+this.uid+'">关闭</a>'+
						'</div>'+
						'<div style="height:'+(config.height-90)+'px;padding:5px 5px; overflow-y:auto;overflow-x:hidden;">'+con+'</div>'+
						'<div style="width:100%;height:30px;padding:10px 0px;line-height:30px;float:left;text-align:center;background:aliceblue">'+
							'<a href="#" style="text-decoration:none;color:black;" id="'+'confirm_'+this.uid+'">确定</a>&nbsp;&nbsp;&nbsp;&nbsp;'+
							(config.type==1?('<a href="#" style="text-decoration:none;color:black;" id="'+'cancel_'+this.uid+'">取消</a>'):'')+
						'</div>'+
					'</div>';
		return digDiv;
	};
	this._initBindEvent= function(){
		var self= this;
		$("#confirm_"+this.uid).bind("click",function(){
			self._confirm(self._$dialog);
			self._destory();
		});
		$("#close_"+this.uid).bind("click",function(){
			self._close(self._$dialog);
			self._destory();
		});
		$("#cancel_"+this.uid).bind("click",function(){
			self._cancel(self._$dialog);
			self._destory();
		})
	};
	this._show= function(){
		this._$dialog= $("#"+this.uid);
		this._letDivCenter();
		this._$dialog.draggable();
	};
	this._destory= function(){
		this._$dialog.remove();
	};
	this._close= function(){
		//do nothing
	};
	this._cancel= function(){
		//do nothing
	};
	this._confirm= function(){}
	this.confirm= function(method){
		if(typeof method=="function"){
			this._confirm= method;
		}else{
			this._confirm= function(){}
		}
		return this;
	};
	this.cancel= function(method){
		if(typeof method=="function"){
			this._cancel= method;
		}else{
			this._cancel= function(){}
		}
		return this;
	}
	this.close= function(method){
		if(typeof method=="function"){
			this._close= method;
		}else{
			this._close= function(){}
		}
		return this;
	}

}